package by.averianov.discounta.webapp.page.tabs;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.CardType;
import by.averianov.discounta.datamodel.CardType_;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.page.administrator.Administrator;
import by.averianov.discounta.webapp.page.create.CreateCardType;
import by.averianov.discounta.webapp.page.table.Row;

public class TabCardType extends Panel {

	private static final Logger LOGGER = LoggerFactory.getLogger(TabCardType.class);
	
	private Long selectId;
	private String currentId;
	
	@Inject
	private UserService userService;
	 
    public TabCardType(String id, final Long selectId, String currentId) {
    	super(id);
    	this.selectId = selectId;
    	this.currentId = currentId;
    	
	}

	private class CardTypeDataProvider extends SortableDataProvider<CardType, SingularAttribute<CardType, ?>> {

		public CardTypeDataProvider() {
			super();
			setSort(CardType_.id, SortOrder.ASCENDING);
		}
	
		@Override
		public Iterator<? extends CardType> iterator(long first, long count) {
	
			SingularAttribute<CardType, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return userService.getAllCardType(sortParam, ascending, (int) first, (int) count).iterator();
		}
	
		@Override
		public long size() {
			return userService.getCardTypeCount();
		}
	
		@Override
		public IModel<CardType> model(CardType cardType) {
			return new CompoundPropertyModel<CardType>(cardType);
		}
	}
    
    @Override
	protected void onInitialize() {
		super.onInitialize();
		
		Link linkAdd = new Link("linkAdd"){
			
			@Override
			public void onClick() {
				setResponsePage(new CreateCardType(null, new PageCreator() {

					@Override
					public Page createPage() {
						return new Administrator(2, 0L);
					}
				}));
				LOGGER.debug("Add Row");
			}
		};
		linkAdd.add(new Label("labelAdd", getButtonAdd()));
		add(linkAdd);

		Link linkChange = new Link("linkChange"){
			@Override
			public void onClick() {
				if(selectId == null) {
					selectId = 0L;
				}
				setResponsePage(new CreateCardType(userService.getCardTypeById(selectId), new PageCreator() {

					@Override
					public Page createPage() {
						return new Administrator(2, selectId);
					}
				}));
				LOGGER.debug("Change Row");
			}
		};
		linkChange.add(new Label("labelChange", getButtonChange()));
		add(linkChange);
		
		Link linkDel = new Link("linkDel"){
			@Override
			public void onClick() {
				
				Administrator page = new Administrator(2, 0L);				
				if(selectId == 0) {
					page.error("Null row selected");
					setResponsePage(page);
				} else {
					userService.removeCardType(selectId);
					LOGGER.debug("Delete Row");					
				}
				setResponsePage(page);
			}
		};
		linkDel.add(new Label("labelDel", getButtonDel()));
		add(linkDel);
		

    	setOutputMarkupId(true);
    	CardTypeDataProvider cardTypeDataProvider = new CardTypeDataProvider();

		final WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body") {
			@Override
			protected void onBeforeRender() {
				super.onBeforeRender();
			}
		};
	
		tableBody.setOutputMarkupId(true);
		add(tableBody);
	
		DataView<CardType> dataView = new DataView<CardType>("list", cardTypeDataProvider, 15) {

			@Override
			protected void populateItem(Item<CardType> item) {
				final CardType cardType = item.getModelObject();
				
                Boolean isSelected = false;
				
				if(selectId == cardType.getId()) {
					isSelected = true;
				}
				
				item.add(new Row("row", cardType, isSelected, currentId));
				item.setVisible(true);				
									
			}
		};
	
		tableBody.add(dataView);	
		add(new PagingNavigator("paging", dataView));	
		add(new OrderByBorder<SingularAttribute<CardType, ?>>("sortById", CardType_.id, cardTypeDataProvider));
		add(new OrderByBorder<SingularAttribute<CardType, ?>>("sortByName", CardType_.nameCardType, cardTypeDataProvider));
	
		
		LOGGER.debug("onInitialize table");
		
		
	}
    

	protected IModel<String> getButtonAdd() {
		return new ResourceModel("p.button.add");
	}
	
	protected IModel<String> getButtonChange() {
		return new ResourceModel("p.button.change");
	}

	protected IModel<String> getButtonDel() {
		return new ResourceModel("p.button.del");
	}
 
}